package com.launcher.minecraft;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class AccountManagerActivity extends AppCompatActivity {

    private Button btnLoginMicrosoft;
    private Button btnCreateOffline;
    private EditText etOfflineUsername;
    private RecyclerView rvAccounts;
    private AccountAdapter accountAdapter;
    private List<Account> accounts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_manager);

        initViews();
        initAccounts();
        setupRecyclerView();
        setupClickListeners();
    }

    private void initViews() {
        btnLoginMicrosoft = findViewById(R.id.btn_login_microsoft);
        btnCreateOffline = findViewById(R.id.btn_create_offline);
        etOfflineUsername = findViewById(R.id.et_offline_username);
        rvAccounts = findViewById(R.id.rv_accounts);
    }

    private void initAccounts() {
        accounts = new ArrayList<>();
        // Load saved accounts from storage
        loadAccounts();
    }

    private void setupRecyclerView() {
        accountAdapter = new AccountAdapter(accounts, new AccountAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Account account) {
                // Set account as selected
                selectAccount(account);
            }

            @Override
            public void onDeleteClick(Account account) {
                // Delete account
                deleteAccount(account);
            }
        });
        rvAccounts.setLayoutManager(new LinearLayoutManager(this));
        rvAccounts.setAdapter(accountAdapter);
    }

    private void setupClickListeners() {
        btnLoginMicrosoft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginWithMicrosoft();
            }
        });

        btnCreateOffline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createOfflineAccount();
            }
        });
    }

    private void loginWithMicrosoft() {
        // Implement Microsoft OAuth login
        Toast.makeText(this, "微软账号登录功能将在后续版本中实现", Toast.LENGTH_LONG).show();
        // In a real implementation, this would open a web view for Microsoft OAuth
    }

    private void createOfflineAccount() {
        String username = etOfflineUsername.getText().toString().trim();
        if (username.isEmpty()) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        // Create offline account
        Account offlineAccount = new Account(username, Account.TYPE_OFFLINE);
        accounts.add(offlineAccount);
        accountAdapter.notifyDataSetChanged();
        
        // Save account
        saveAccount(offlineAccount);
        
        // Clear input
        etOfflineUsername.setText("");
        
        Toast.makeText(this, "离线账号创建成功", Toast.LENGTH_SHORT).show();
    }

    private void selectAccount(Account account) {
        // Mark account as selected
        for (Account acc : accounts) {
            acc.setSelected(false);
        }
        account.setSelected(true);
        accountAdapter.notifyDataSetChanged();
        
        // Save selected account
        saveSelectedAccount(account);
        
        Toast.makeText(this, "已选择账号: " + account.getUsername(), Toast.LENGTH_SHORT).show();
    }

    private void deleteAccount(Account account) {
        accounts.remove(account);
        accountAdapter.notifyDataSetChanged();
        
        // Delete from storage
        deleteAccountFromStorage(account);
        
        Toast.makeText(this, "账号已删除", Toast.LENGTH_SHORT).show();
    }

    private void loadAccounts() {
        // In a real implementation, this would load accounts from storage
        // For now, we'll add a sample offline account
        accounts.add(new Account("Player1", Account.TYPE_OFFLINE));
    }

    private void saveAccount(Account account) {
        // In a real implementation, this would save the account to storage
    }

    private void saveSelectedAccount(Account account) {
        // In a real implementation, this would save the selected account to storage
    }

    private void deleteAccountFromStorage(Account account) {
        // In a real implementation, this would delete the account from storage
    }

    // Inner class for Account
    public static class Account {
        public static final int TYPE_OFFLINE = 0;
        public static final int TYPE_MICROSOFT = 1;
        
        private String username;
        private int type;
        private boolean isSelected;
        
        public Account(String username, int type) {
            this.username = username;
            this.type = type;
            this.isSelected = false;
        }
        
        // Getters and setters
        public String getUsername() {
            return username;
        }
        
        public void setUsername(String username) {
            this.username = username;
        }
        
        public int getType() {
            return type;
        }
        
        public void setType(int type) {
            this.type = type;
        }
        
        public boolean isSelected() {
            return isSelected;
        }
        
        public void setSelected(boolean selected) {
            isSelected = selected;
        }
    }
}
